package com.bw.lib_mvicore.ui

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.jaeger.library.StatusBarUtil

/**
 * @Author : ZhouZiYao
 * @Time : On 2023/11/17 16:21
 * @Description :
 */
abstract class BaseActivity: AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        injectARout()
        super.onCreate(savedInstanceState)
        if (-1 == getLayoutId()) {
            setContentView(getLayoutView())//viewBinding
        }else{
            setContentView(getLayoutId())//普通
        }

        //状态栏
        StatusBarUtil.setColorNoTranslucent(this, generateStatusBarColor())
        initEnv()
        initView()
        initData()
        initEvent()
    }

    /**
     * 初始化事件
     */
    protected open fun initEvent() {

    }

    /**
     * 初始化视图
     */
    protected open fun initView() {

    }

    /**
     * 初始化数据
     */
    protected open fun initData() {

    }

    /**
     * 初始化环境
     */
    protected open fun initEnv() {

    }

    /**
     * 设置状态栏颜色
     */
    protected open fun generateStatusBarColor(): Int{
        return Color.TRANSPARENT
    }

    /**
     * ARout注入，接收数据
     */
    protected open fun injectARout() {

    }

    /**
     * 返回布局view，配合ViewBinding使用
     */
    abstract fun getLayoutView(): View

    /**
     * 返回布局id
     */
    protected open fun getLayoutId(layoutId: Int = -1): Int{
        return layoutId
    }

    private val handler: Handler = Handler(Looper.getMainLooper())

    /**
     * 提示信息
     */
    fun showMsg(msg: String){
        if (Looper.getMainLooper().thread == Thread.currentThread()){
            Log.d("TAG", "showMsg: $msg")
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
        }else{
            handler.post{
                Log.d("TAG", "showMsg: $msg")
                Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
            }
        }
    }

    /**
     * 跳转到指定页面
     */
    fun routeActivity(target: Class<*>){
        startActivity(Intent(this, target))
    }

    /**
     * 跳转到指定页面并传递参数
     */
    fun routActivity(target: Class<*>, params: Bundle){
        val intent = Intent(this, target)
        intent.putExtra("params", params)
        startActivity(intent)
    }

    fun addFragment(fragment: Fragment, tag: String){
        supportFragmentManager.beginTransaction().add(fragment,tag).commitAllowingStateLoss()
    }

    fun hideFragment(tag: String){
        supportFragmentManager.findFragmentByTag(tag)
            ?.let {
                supportFragmentManager.beginTransaction().hide(it).commitAllowingStateLoss()
            }
    }

    fun showFragment(tag: String){
        supportFragmentManager.findFragmentByTag(tag)?.let {
            supportFragmentManager.beginTransaction().show(it).commitAllowingStateLoss()
        }
    }

    fun replaceFragment(containerId: Int, fragment: Fragment){
        supportFragmentManager.beginTransaction().replace(containerId, fragment).commitAllowingStateLoss()
    }

    fun switchFragment(fl: Int, from: Fragment?, to: Fragment){
        if (!to.isAdded()){
            supportFragmentManager.beginTransaction().hide(from!!).add(fl, to).commit()
        } else{
            supportFragmentManager.beginTransaction().hide(from!!).show(to).commit()
        }
    }

    fun closeThis(){
        finish()
    }

}